package com.brooks.demo;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * cyclicBarrier 循环栅栏
 * 等线程数够了再执行
 * @Author mihutu
 * @Date 2020/10/19 10:05
 * @Version 1.0
 */
public class DemoCyclicBarrier {
    public static void main(String[] args) {
        //lambda写法
//        CyclicBarrier cyclicBarrier = new CyclicBarrier(20, ()-> System.out.println("人满了，发车咯"));
        CyclicBarrier cyclicBarrier = new CyclicBarrier(20, new Runnable() {
            @Override
            public void run() {
                System.out.println("人满了，发车咯");
            }
        });

        for(int i=0;i<10;i++){
            new Thread(()->{
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }


}
